package com.martin.lib_base.binding.adapter

import android.view.View
import androidx.databinding.BindingAdapter

/**
 * 设置View的可用事件
 */
@BindingAdapter("setEnable")
fun setEnable(view: View, enabled: Boolean) {
    view.isEnabled = enabled
}

/**
 * 设置View的显示状态
 */
@BindingAdapter("setVisible")
fun setVisible(view: View, visible: Any?) {
    if (visible == null) {
        view.visibility = View.GONE
        return
    }
    when (visible) {
        is Boolean ->
            view.visibility = if (visible) View.VISIBLE else View.GONE

        is Int ->
            view.visibility = if (visible == 0) View.GONE else View.VISIBLE

        is String ->
            view.visibility = if (visible.isEmpty()) View.GONE else View.VISIBLE

        else -> {
            view.visibility = View.VISIBLE
        }
    }
}